#include "TimeUtil.h"
#include "system_data.h"
/* convert 20071017 to time_t */
time_t day_str2_ulong(const char *TimeStr)
{
	struct tm cur;
	char year[5] = "";
	char mon[3] = "";
	char day[3] = "";

	memset(year,0,sizeof(year));
	memcpy(year,TimeStr,4);

	memset(mon,0,sizeof(mon));
	memcpy(mon,TimeStr+4,2);

	memset(day,0,sizeof(day));
	memcpy (day,TimeStr+6,2);

	cur.tm_sec = 0;
	cur.tm_min = 0;
	cur.tm_hour = 0;

	cur.tm_mday = atoi(day);
	cur.tm_mon = atoi(mon) - 1;
	cur.tm_year = atoi(year) - 1900;

	return mktime(&cur);
}

/* convert 20071017101010 to time_t */
time_t sec_str2_ulong(const char *TimeStr)
{
	struct tm cur;

	char year[5] = "";
	char mon[3] = "";
	char day[3] = "";
	char hour[3] = "";
	char min[3] = "";
	char sec[3] = "";

	memset(year,0,sizeof(year));
	memcpy(year,TimeStr,4);

	memset(mon,0,sizeof(mon));
	memcpy(mon,TimeStr+4,2);

	memset(day,0,sizeof(day));
	memcpy (day,TimeStr+6,2);

	memset(hour,0,sizeof(day));
	memcpy (hour,TimeStr+8,2);

	memset(min,0,sizeof(day));
	memcpy (min,TimeStr+10,2);

	memset(sec,0,sizeof(day));
	memcpy (sec,TimeStr+12,2);

	cur.tm_sec = atoi(sec);
	cur.tm_min = atoi(min);
	cur.tm_hour = atoi(hour);

	cur.tm_mday = atoi(day);
	cur.tm_mon = atoi(mon) - 1;
	cur.tm_year = atoi(year) - 1900;

	return mktime(&cur);
}

/* convert 2007-10-17 10:10:10 to time_t */
time_t nsec_str2_ulong(const char *TimeStr)
{
	struct tm cur;

	char year[5] = "";
	char mon[3] = "";
	char day[3] = "";
	char hour[3] = "";
	char min[3] = "";
	char sec[3] = "";

	memset(year,0,sizeof(year));
	memcpy(year,TimeStr,4);

	memset(mon,0,sizeof(mon));
	memcpy(mon,TimeStr+5,2);

	memset(day,0,sizeof(day));
	memcpy (day,TimeStr+8,2);

	memset(hour,0,sizeof(day));
	memcpy (hour,TimeStr+11,2);

	memset(min,0,sizeof(day));
	memcpy (min,TimeStr+14,2);

	memset(sec,0,sizeof(day));
	memcpy (sec,TimeStr+17,2);

	cur.tm_sec = atoi(sec);
	cur.tm_min = atoi(min);
	cur.tm_hour = atoi(hour);

	cur.tm_mday = atoi(day);
	cur.tm_mon = atoi(mon) - 1;
	cur.tm_year = atoi(year) - 1900;

	return mktime(&cur);
}

/* convert time_t to 20071007 format */
char * ulong2_day_str(time_t time)
{
	struct tm *date;
	static char buff[17];
	memset(buff,0,sizeof(buff));
	date = localtime(&time);

	sprintf(buff,"%4d%02d%02d",date->tm_year + 1900, date->tm_mon + 1,date->tm_mday);
	return buff;
}

/* convert time_t to 20071007000000 format */
char * ulong2_sec_str(time_t time)
{
	struct tm *date;
	static char buff[17];
	memset(buff,0,sizeof(buff));
	date = localtime(&time);

	sprintf(buff,"%4d%02d%02d%02d%02d%02d",date->tm_year + 1900, date->tm_mon + 1,date->tm_mday,date->tm_hour,date->tm_min,date->tm_sec);

	return buff;
}

/* convert time_t to 2007-10-07 00:00:00 format */
char * uong2_nsec_str(time_t time)
{
	struct tm *date;
	static char buff[17];
	memset(buff,0,sizeof(buff));
	date = localtime(&time);

	sprintf(buff,"%4d-%02d-%02d %02d:%02d:%02d",date->tm_year + 1900, date->tm_mon + 1,date->tm_mday,date->tm_hour,date->tm_min,date->tm_sec);

	return buff;
}